package com.fz.us.admin.base.service.common;

import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.utils.mapper.JsonMapper;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-20 。
 * ============================================================================
 */
@Service
public class ResultService {

    public Result build(int state, String message, Object data, HttpStatus statusCode) {
        return new Result(state, message, data, statusCode);
    }

    public Result success(String message, Object data){
        return build(1, message, data, HttpStatus.OK);
    }

    public Result success(String message){
        return success(message, null);
    }

    public Result success(){
        return success("操作成功");
    }

    public Result successWithData(Object data){
        return success("操作成功", data);
    }

    public Result successWithId(String id){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);

        return success("操作成功", map);
    }

    public Result successWithByte(String fileName, byte[] bytes){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fileName", fileName);
        map.put("bytes", bytes);

        return success("操作成功", map);
    }

    public Result fail(String message){
        return build(0, message, null, HttpStatus.OK);
    }

    public Result fail(){
        return fail("操作失败");
    }

    public Result error(HttpStatus statusCode, String message){
        return build(0, message, null, statusCode);
    }

    public String getBody(Result result) {
        return JsonMapper.nonEmptyMapper().toJson(result);
    }

}
